<template>
  <div>
    <router-link :to="'/rbac/user/add'">
      <el-button type="primary">添加</el-button>
    </router-link>
    <el-table
      ref="multipleTable"
      :data="list"
      tooltip-effect="dark"
      style="width: 100%">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="password" label="密码"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column prop="phone" label="电话"> </el-table-column>
       <el-table-column label="操作">
      <template slot-scope="scope">
        <router-link :to="'/rbac/user/update/' + scope.row.id">
          <el-button type="primary" size="mini">修改</el-button>
        </router-link>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
    </el-table>
    <el-pagination
      @current-change="selectByPage"
      :current-page="page"
      :page-size="limit"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>
  
<script>
//引入user.js
import userApi from "@/api/rbac/user";

export default {
  name: "",
  data() {
    return {
      list: null,//当前页的数据
      page: 1,//当前页
      limit: 5,//每页有多少条
      total: 0//总记录数
    };
  },
  created() {
    this.selectByPage(this.page)
    /*userApi.selectByPage(this.page, this.limit).then(result => {
      console.log(result)
      this.list = result.data.list
      this.total = result.data.total
    })*/
  },
  methods: {
    selectByPage(page=1) {
      //页面切换时候会传递过来第几页
      this.page = page
      userApi.selectByPage(this.page, this.limit).then(result => {
        console.log(result)
        this.list = result.data.list
        //Invalid prop: type check failed for prop "total". Expected Number with value 8, got String with value "8".
        //把long类型转换成Number类型
        this.total = result.data.total - 0
      })
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm('您确认要删除么?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          userApi.deleteById(row.id).then(result => {
            if (result.ok) {
              this.$message({
                type: 'success',
                message: result.msg
              })
              //删除完了之后，重新查询最新的数据
              this.selectByPage()
            } else {
              this.$message({
                type: 'error',
                message: result.msg
              })
            }
          })
          
        })
    }
  },
};
</script>
 
<style scoped>
</style>